import { defHttp } from '/@/utils/http/axios';

import { ErrorMessageMode, UploadFileParams } from '/#/axios';
import {
  UserInfo,
  UserList,
  UserModel,
  UserPageListResultModel,
  UserPageListSearchModel,
} from './model';
import { useGlobSetting } from '/@/hooks/setting';

const globSetting = useGlobSetting();

enum Api {
  Page = '/system/user/page',
  Modify = '/system/user/modify',
  Delete = '/system/user/delete',



  List = '/organization/user/list',
  Info = '/organization/user/info',
  UpdateInfo = '/organization/user/update/info',
  UpdatePassword = '/organization/user/update/password',
  UpdateAvatar = '/organization/user/update/avatar',
  User = '/organization/user',
  MultiInfo = '/organization/user/info/multi',
  Password = '/organization/user/reset-password',
}

/**
 * @description: 查询用户分页列表
 */
export function getUserPageList(params: any, mode: ErrorMessageMode = 'message') {
  return defHttp.get(
    { url: Api.Page, params },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: 修改用户
 */
export async function Modify(role: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<number>(
    {
      url: Api.Modify,
      params: role,
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: 删除用户
 */
export async function Delete(role: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<number>(
    {
      url: Api.Delete,
      params: role,
    },
    {
      errorMessageMode: mode,
    },
  );
}





/**
 * @description: 删除用户（批量删除）
 */
export async function deleteUser(ids: string[], mode: ErrorMessageMode = 'modal') {
  return defHttp.delete<number>(
    {
      url: Api.User,
      data: ids,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 新增用户
 */
export async function addUser(user: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.post<number>(
    {
      url: Api.User,
      params: user,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 获取用户信息
 */
export async function getUser(id: String, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<UserModel>(
    {
      url: Api.Info,
      params: { id },
    },
    {
      errorMessageMode: mode,
    },
  );
}
/**
 * @description: 重置用户密码
 */
export async function resetUserPassword(id: String, mode: ErrorMessageMode = 'modal') {
  return defHttp.put(
    {
      url: Api.Password,
      data: { id },
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新用户
 */
export async function updateUser(user: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.put<number>(
    {
      url: Api.User,
      params: user,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新当前登录用户信息
 */
export async function updateUserInfo(user: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.put<number>(
    {
      url: Api.UpdateInfo,
      params: user,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 修改用户密码
 */
export async function updatePassword(user: Recordable, mode: ErrorMessageMode = 'modal') {
  return defHttp.put<number>(
    {
      url: Api.UpdatePassword,
      params: user,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 更新头像
 */
export function uploadAvatar(
  params: UploadFileParams,
  onUploadProgress: (progressEvent: ProgressEvent) => void,
) {
  return defHttp.uploadFile<string>(
    {
      url: Api.UpdateAvatar,
      baseURL: globSetting.apiUrl,
      method: 'POST',
      onUploadProgress,
    },
    params,
  );
}

/**
 * @description: 批量获取用户信息
 */
export async function getUserMulti(ids: String, mode: ErrorMessageMode = 'modal') {
  return defHttp.get<UserInfo[]>(
    {
      url: Api.MultiInfo,
      params: { ids },
    },
    {
      errorMessageMode: mode,
    },
  );
}

export async function getUserList(
  params: UserPageListSearchModel,
  mode: ErrorMessageMode = 'modal',
) {
  return defHttp.get<UserList>(
    {
      url: Api.Page,
      params,
    },
    {
      errorMessageMode: mode,
    },
  );
}

/**
 * @description: 查询所有人员信息
 */
export async function getAllUserList(mode: ErrorMessageMode = 'modal') {
  return defHttp.get<UserInfo[]>(
    {
      url: Api.List,
    },
    {
      errorMessageMode: mode,
    },
  );
}
